package com.imooc.luckymoney.client;

import com.alibaba.fastjson.JSONObject;
import com.imooc.luckymoney.entity.Luckymoney;
import com.imooc.luckymoney.entity.Result;
import com.imooc.luckymoney.httpClient.HttpClient;
import com.imooc.luckymoney.util.LuckymoneyJsonUtil;
import com.imooc.luckymoney.util.MathUtil;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.math.BigDecimal;


public class Client extends Application {
    private final TextField writeMoneyField = new TextField();
    private final TextField writeProducerField = new TextField();
    private final TextField statementField = new TextField();
    private final TextField idField = new TextField();
    private final TextField showMoneyField = new TextField();
    private final TextField showProducerField = new TextField();
    private final TextField showConsumerField = new TextField();
    @Override
    public void start(Stage primaryStage){

        GridPane rightGridPane = new GridPane();
        rightGridPane.add(new Label("id:"),0,0);
        rightGridPane.add(new Label("money:"),0,1);
        rightGridPane.add(new Label("consumer:"),0,2);
        rightGridPane.add(new Label("producer:"),0,3);
        rightGridPane.add(idField,1,0);
        rightGridPane.add(showMoneyField,1,1);
        rightGridPane.add(showConsumerField,1,2);
        rightGridPane.add(showProducerField,1,3);
        statementField.setDisable(true);
        showConsumerField.setDisable(true);
        showMoneyField.setDisable(true);

        GridPane leftGridPane = new GridPane();
        leftGridPane.add(new Label("money:"),0,0);
        leftGridPane.add(new Label("producer:"),0,1);
        leftGridPane.add(new Label("statement:"),0,2);
        leftGridPane.add(writeMoneyField,1,0);
        leftGridPane.add(writeProducerField,1,1);
        leftGridPane.add(statementField,1,2);
        showProducerField.setDisable(true);

        Button submit = new Button("submit");
        submit.setOnAction(new SubmitHandler());
        HBox hBox = new HBox(10);
        hBox.setPadding(new Insets(10,10,10,10));
        hBox.getChildren().add(submit);

        Button search = new Button("search");
        search.setOnAction(new searchHandler());
        HBox hBox1 = new HBox(10);
        hBox1.setPadding(new Insets(10,10,10,10));
        hBox1.getChildren().add(search);

        BorderPane left = new BorderPane();
        left.setCenter(leftGridPane);
        leftGridPane.setAlignment(Pos.CENTER);
        left.setBottom(hBox);
        hBox.setAlignment(Pos.CENTER);

        BorderPane right = new BorderPane();
        right.setCenter(rightGridPane);
        rightGridPane.setAlignment(Pos.CENTER);
        right.setBottom(hBox1);
        hBox1.setAlignment(Pos.CENTER);

        GridPane center = new GridPane();
        center.setHgap(50);
        center.add(left,0,0);
        center.add(right,1,0);
        Scene scene = new Scene(center,600,200);
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.setTitle("luckymoney");
        primaryStage.show();
    }
    class SubmitHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            StringBuilder sbd = new StringBuilder();
            sbd.append("money=");
            String money = writeMoneyField.getText();
            try{
                Double.valueOf(money);
            }catch (Exception e){
                System.out.println("金额输入有误");
                statementField.setText("failed");
                return;
            }
            sbd.append(money);
            sbd.append("&producer=");
            sbd.append(writeProducerField.getText());
            String JSONResult = HttpClient.doPost("http://localhost:8081/luckymoney/luckymoneys/create", sbd.toString());
            Result result = LuckymoneyJsonUtil.getResult(JSONResult, Result.class);
            if(result != null) {
                if (result.getCode() == 0) {
                    statementField.setText("success");
                    System.out.println(result.getData());
                } else {
                    System.out.println("code:"+result.getCode());
                    System.out.println("msg:"+result.getMsg());
                    System.out.println("reason:"+result.getData());
                    statementField.setText("failed");
                }
            }else{
                statementField.setText("failed");
            }
        }
    }

    class searchHandler implements EventHandler<ActionEvent>{

        @Override
        public void handle(ActionEvent event) {
            boolean successful = false;
            StringBuilder sbd = new StringBuilder();
            sbd.append("id=");
            String id =idField.getText();
            if(MathUtil.isNumeric(id)){
                sbd.append(id);
            }else {
                System.out.println("参数输入有误\n");
                return;
            }
            String JSONResult = HttpClient.doGet("http://localhost:8081/luckymoney/luckymoneys/find",sbd.toString());
            Result result = LuckymoneyJsonUtil.getResult(JSONResult, Result.class);
            if (result!= null){
                if(result.getCode() == 0){
                    JSONObject jsonObject =(JSONObject)result.getData();
                    Luckymoney luckymoney = new Luckymoney();
                    luckymoney.setMoney(jsonObject.getBigDecimal("money"));
                    luckymoney.setConsumer(jsonObject.getString("consumer"));
                    luckymoney.setProducer(jsonObject.getString("producer"));
                    showMoneyField.setText(luckymoney.getMoney().toString());
                    showConsumerField.setText(luckymoney.getConsumer());
                    showProducerField.setText(luckymoney.getProducer());
                    successful = true;
                }else {
                    System.out.println("code:"+result.getCode()+"\nmsg:"+result.getMsg()+"\nreason:"+result.getData());
                }
            }
            if(!successful){
                showMoneyField.clear();
                showProducerField.clear();
                showConsumerField.clear();
            }
        }
    }
}

